package gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

public class QRPanel extends JPanel implements MouseListener
{
	private static final long serialVersionUID = -7131697544886767814L;
	BufferedImage myImage = null;
	int multi = 1;
	int mWidth = 1;
	int mHeight = 1;
	int offX = 0;
	int offY = 0;
	
	private void recalc()
	{
		int width = getWidth();
		int height = getHeight();
		
		if(myImage != null)
		{
			multi = height / myImage.getHeight();
			if(multi <= 0)
				multi = 1;
			
			mWidth = myImage.getWidth() * multi;
			mHeight = myImage.getHeight() * multi;
			
			offX = (width - mWidth) / 2;
			offY = (height - mHeight) / 2;
		}
	} 
	
	public QRPanel(BufferedImage image)
	{
		super(true);
		myImage = image;
		
		//GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
		//int width = gd.getDisplayMode().getWidth();
		//int height = gd.getDisplayMode().getHeight();
		
		recalc();
		//setSize(mWidth, mHeight);
		
		addMouseListener(this);
	}
	
	public void setImage(BufferedImage img)
	{
		myImage = img;
		recalc();
		repaint();
	}
	
	public void paint(Graphics g)
	{
		recalc();
		g.setColor(Color.white);
		g.clearRect(0, 0, getWidth(), getHeight());
		
		if(myImage != null)
			g.drawImage(myImage, offX, offY, mWidth, mHeight, null);
	}

	@Override
	public void mouseClicked(MouseEvent arg0)
	{
		//setVisible(false);
		if(myImage != null)
			new QRDisplay(myImage);
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {}
	@Override
	public void mouseExited(MouseEvent arg0) {}
	@Override
	public void mousePressed(MouseEvent arg0) {}
	@Override
	public void mouseReleased(MouseEvent arg0) {}
}
